/**@author: Administrator
 * @date: 2013-3-11
 * @Description:
 */
package org.benxiong.iplay.ui.fragment;

import org.benxiong.iplay.R;
import org.benxiong.iplay.util.GlobalVar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.Process;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

/**
 * @author Administrator
 *
 */
public class SettingFragment extends PreferenceFragment implements OnPreferenceClickListener, OnSharedPreferenceChangeListener{
	SharedPreferences			prefs;
	private String[]			unitArray;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		addPreferencesFromResource(R.xml.setting);
		prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
		
		this.findPreference("quit").setOnPreferenceClickListener(this);
		
		findPreference("hasnew").setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			@Override
			public boolean onPreferenceClick(Preference preference) {
				//check new Version
				new AlertDialog.Builder(SettingFragment.this.getActivity())
				.setMessage("Find New Version")
				.create()
				.show();
				
				return true;
			}
		});
	}
	
	

	/* (non-Javadoc)
	 * @see android.preference.Preference.OnPreferenceClickListener#onPreferenceClick(android.preference.Preference)
	 */
	@Override
	public boolean onPreferenceClick(Preference preference) {
		if ("quit".equals(preference.getKey())) {
			//show quit alert dialog
			AlertDialog dialog = new AlertDialog.Builder(this.getActivity()).setMessage(R.string.quit_alert)
			.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					for (Activity act : GlobalVar.activityList) {
						act.finish();
					}
					Process.killProcess(Process.myPid());
				}
			})
			.setNegativeButton(android.R.string.cancel, null)
			.create();
			dialog.show();
		}
		
		return false;
	}



	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		bindData();
	}


	private void bindData() {
		int unitIndex = Integer.valueOf(prefs.getString("unit", "0"));

		unitArray = this.getResources().getStringArray(R.array.unit);

		findPreference("unit").setSummary(unitArray[unitIndex]);
	}

	/* (non-Javadoc)
	 * @see android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged(android.content.SharedPreferences, java.lang.String)
	 */
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Preference pref = findPreference(key);
		// TODO Auto-generated method stub
		if (pref instanceof ListPreference) {
			ListPreference lsp = (ListPreference) pref;
			pref.setSummary(lsp.getEntry());
		} 
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}
}
